package com.pu.controller.admin;

import com.pu.dto.SetmealDTO;
import com.pu.dto.SetmealPageQueryDTO;
import com.pu.result.PageResult;
import com.pu.result.Result;
import com.pu.service.SetmealService;
import com.pu.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Yusc
 * @Date 2023/10/28 17:33
 * @Version
 * @Description 套餐管理
 * 优化：当套餐发生变化时，需要清理缓存，防止两端数据不一致 —— 用spring-cache框架实现
 **/
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * @param
     * @return
     * @auther: Yusc
     * @date 2023/10/28 17:40
     * @Description 新增套餐
     */
    @PostMapping
    @ApiOperation("新增套餐")
    // 将一条或多条数据从缓存中删除
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐：{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * @param setmealPageQueryDTO
     * @auther: Yusc
     * @date 2023/10/28 20:22
     * @Description 套餐分页查询
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询:{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param ids
     * @auther: Yusc
     * @date 2023/10/28 20:55
     * @Description 批量删除套餐
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    // 清除setmealCache中的所有缓存数据
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除套餐 ids:{}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * @param id
     * @auther: Yusc
     * @date 2023/10/28 22:24
     * @Description 根据id查询套餐，用于修改页面回显数据
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐 id:{}", id);
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }

    /**
     * @param setmealDTO
     * @auther: Yusc
     * @date 2023/10/28 22:33
     * @Description 修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐")
    // 清除setmealCache中的所有缓存数据
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐:{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * @param status
     * @param id
     * @auther: Yusc
     * @date 2023/10/28 22:43
     * @Description 套餐起售停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    // 清除setmealCache中的所有缓存数据
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("套餐起售停售 status:{},id:{}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success();
    }


}
